$(document).ready(function () {
    // 出发地

    var bannerH = $(".swiper-container").offset().height;
    var formH = $(".form").offset().height;
    var marginH=bannerH+formH+7;
    var p = 1;
    $(".wrapper").css("margin-top",marginH+'px');
    var form = $("form").eq(0);
    var select = $("select", form);
    // banner图
    var mySwiper = new Swiper('.swiper-container', {
        autoplay: 2000,//可选选项，自动滑动
        loop: true,//可选选项，开启循环
        pagination : '.swiper-pagination',
    })
    // 上拉下拉



    var li1 = $(".wrapper .ht-content").eq(0);
    tops = li1.offset().height + 20;
    $(".scroller").off().on("swipe", (function (e) {
        var li1 = $(".wrapper .ht-content").eq(0);
        var H = li1.offset().height;
        var a = li1.offset().top;
        var len = $(".wrapper .ht-content").length;
        var i = 0;
        if (a < tops) {
            myScroll.refresh();
        }
        if (a > tops) {
            if ($(".top").length) {

            } else {
                $("<div class='top' style='line-height:30px;color:#999;font-size:14px'>&nbsp;<img src='"+img+"/3.gif' style='margin-left:40%;margin-top:0.5em;height:1em;float:left;'/>更新中...</div>").insertBefore(li1);
                setTimeout(jiazai, 500);
                function jiazai() {
                    setTimeout(jian, 200);
                }

                function jian() {
                    var ji = "<img src='"+img+"/123.gif' class='jiantou' style='margin-left:40%;height:3em;'/>"
                    $(".top").replaceWith(ji);
                    setTimeout(shanchu, 0)
                }

                function shanchu() {
                    // 初始化 p
                    p=1;
                    start_city = demo1obj.value;
                    end_city   = demo2obj.value;
                    length     = demo3obj.value;

                    if(start_city=='出发地' || start_city=='不限'){
                        start_city='';
                    }
                    if(end_city=='目的地' || end_city=='不限'){
                        end_city='';
                    }
                    if(length=='车长' || length=='不限'){
                        length='';
                    }



                    $.ajax({
                        type: "post",
                        // url: "ajaxList",
                        url: "dataList",
                        // data: {sheng: $("#sheng").val(),length:$('#length').val()},
                        data:{'start_city':start_city,'end_city':end_city,'length':length},
                        dataType: "html",
                        success: function (r) {
                            $('#list').html(r);
                            myScroll.refresh();
                        }
                    })     
                }
            }
        }

//底部部分   效果更改   依赖iscroll.js,必须position:absolute;top和bottom用来设定滚动区域显示大小,用滚动条判断,来到达底部
        var aa = myScroll.maxScrollY;//滚动条最底部
        var bb = $(".iScrollIndicator").eq(0).offset().top;//滚动条到屏幕顶端的距离
        var cc = $(".iScrollIndicator").eq(0).height();//滚动条的高度
        var headH=$(".ht-bar").eq(0).offset().height; //头部的高度
        var navH=$(".form").eq(0).offset().height;//导航栏高度
        var dd = $(".wrapper").height() + headH+navH-20;
        var ee = $(".ht-list").height();
        var formTop = $(".form").offset().top;//form到顶部的距离
        if (formTop <= 37) {
            var liFirst = $(".ht-content").eq(0);
            alert(liFirst)
            $(".form").insertBefore($(".conOne").eq(0));
            $(".wrapper").css({top: "60px"});
        }
        if ((bb + cc ) >= dd) {
            
            console.log("到底了,后台数据放这就可以");//到底了
            $(".wrapper").css({MarginTop:"53px"})
            $(".swiper-container").remove()
            console.log(aa, bb, cc, dd, ee, formTop)

            var BottomTip = "<div class='di' style='line-height:30px;color:#999;font-size:14px'><img src='"+img+"/3.gif' style='margin-left:40%;margin-top:0.5em;height:1em;float:left;'/>上拉刷新...</div>";
            if($(".ht-list .di").length==0){
                $(".ht-list").append(BottomTip);
            }

            myScroll.scrollBy(0, -124, 1);
            setTimeout(function () {

                // alert(234);
                $(".ht-list .di").remove();
                //检查条件
                start_city = demo1obj.value;
                end_city   = demo2obj.value;
                length     = demo3obj.value;

                if(start_city=='出发地' || start_city=='不限'){
                    start_city='';
                }
                if(end_city=='目的地' || end_city=='不限'){
                    end_city='';
                }
                if(length=='车长' || length=='不限'){
                    length='';
                }
                
                pageobj=document.getElementById('page');
                page=pageobj.value;  //总页数
                
                console.log(p+'index.js');
                if(p>page){
                    p=page;
                } 
                $.ajax({
                    type: "post",
                    url: "dataList",
                    data:{'start_city':start_city,'end_city':end_city,'length':length,'page':++p},
                    dataType: "html",
                    success: function (r) {
                        $('#list').append(r);
                        myScroll.refresh();
                    }
                })
                myScroll.refresh();
            }, 500);


        }

        var bannerTu = $(".swiper-container");

        // 图片懒加载


        Echo.init({
            offset: 0,//离可视区域多少像素的图片可以被加载
            throttle: 200 //图片延时多少毫秒加载
        });

        myScroll.refresh();
    }))

    // 图片懒加载
    Echo.init({
        offset: 0,//离可视区域多少像素的图片可以被加载
        throttle: 0 //图片延时多少毫秒加载
    });
})

